package com.tedu.ds.base.listener;

import com.tedu.ds.base.enmu.EmpStatus;
import com.tedu.ds.base.response.JsonResult;
import com.tedu.ds.employee.controller.EmployeeController;
import com.tedu.ds.employee.pojo.dto.EmployeeSaveParam;
import com.tedu.ds.employee.pojo.vo.EmployeeVO;
import com.tedu.ds.employee.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@Component
public class EmpSessionTimeoutListener implements HttpSessionListener {
    @Autowired
    private EmployeeService employeeService;
    @Override
    public void sessionCreated(HttpSessionEvent se) {
        HttpSessionListener.super.sessionCreated(se);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        HttpSessionListener.super.sessionDestroyed(se);
        HttpSession session=se.getSession();
        EmployeeVO employeeVO = (EmployeeVO) session.getAttribute("employeeVO");
        if (employeeVO!=null){
            EmployeeSaveParam employee=new EmployeeSaveParam() ;
            BeanUtils.copyProperties(employeeVO,employee);
            employee.setStatus(EmpStatus.ENABLE.getCode());
            employeeService.save(employee);
        }

    }
}
